﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using memo;
using memoVM;
using Microsoft.Win32;

namespace memoUI
{
    /// <summary>
    /// Interaction logic for ContentWindow.xaml
    /// </summary>
    public partial class ContentWindow : Window
    {
        private int NumberOfContent;

        private Button[] DynamicListButtons;

        private ViewModel _ViewModel;

        private int textBlockHeight = 30;
        private Content myContent;

        private Image ContentImage;
        private StackPanel ContentImagePanel;

        public ContentWindow()
        {
            InitializeComponent();

            _ViewModel = ViewModel.Instance;
            NumberOfContent = _ViewModel.GetNumberOfContents();

            DynamicListButtons = new Button[NumberOfContent];

            if (NumberOfContent > 0)
            {
                Init();
            }
        }

        private void MenuButton_Click(object sender, RoutedEventArgs e)
        {
            SubTopicSelectorWindow Window = new SubTopicSelectorWindow();
            Window.Show();
            this.Close();
        }

        private void KnowledgeTestButton_Click(object sender, RoutedEventArgs e)
        {
            KnowledgeTestWindow Window = new KnowledgeTestWindow();
            Window.Show();
            this.Close();
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (_ViewModel.DeleteSelectedContent())
            {
                Init();
            }
            else
            {
                MessageBox.Show("error (╯°□°）╯︵ ┻━┻");
            }
            ContentWindow window = new ContentWindow();
            window.Show();
            Close();
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            if (_ViewModel.SelectedContentsIsNotEmpty())
            {
                ContentEditorWindow Window = new ContentEditorWindow(false);
                Window.ShowDialog();
                ContentWindow window = new ContentWindow();
                window.Show();
                Close();
            }
            else
            {
                MessageBox.Show("Error, no contents. Make a new one first!");
            }
            
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            ContentEditorWindow Window = new ContentEditorWindow(true);
            Window.ShowDialog();
            ContentWindow window = new ContentWindow();
            window.Show();
            Close();
        }

        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            if (_ViewModel.SetNextContent())
            {
                SetCurrentContent();
            }
        }

        private void PreviousButton_Click(object sender, RoutedEventArgs e)
        {
            if (_ViewModel.SetPreviousContent())
            {
                SetCurrentContent();
            }
        }

        private void ListElement_Click(object sender, RoutedEventArgs e)
        {
            Button obj = sender as Button;
            _ViewModel.SelectedContent = int.Parse(obj.Tag.ToString());
            SetCurrentContent();
        }

        private void Init()
        {

            NumberOfContent = _ViewModel.GetNumberOfContents();

            _ViewModel.SelectedContent = 0;
            SetCurrentContent();
        }

        public void SetCurrentContent()
        {
            ContentNameTextBlock.Text = _ViewModel.GetSelectedContent().Title;
            TopicAndSubTopicNameTextBlock.Text = _ViewModel.GetTopicAndSubtopicName();
            ContentTextBox.Text = _ViewModel.GetSelectedContent().Details;
            ContentList.Children.Clear();
            NumberOfContent = _ViewModel.GetNumberOfContents();

            DynamicListButtons = new Button[NumberOfContent];

            for (int i = 0; i < NumberOfContent; i++)
            {
                myContent = _ViewModel.GetContentAtIndex(i);

                DynamicListButtons[i] = new Button();
                DynamicListButtons[i].Content = myContent.Title;
                DynamicListButtons[i].Height = textBlockHeight;
                DynamicListButtons[i].HorizontalAlignment = HorizontalAlignment.Stretch;
                DynamicListButtons[i].VerticalAlignment = VerticalAlignment.Top;
                DynamicListButtons[i].Tag = i;
                DynamicListButtons[i].Click += ListElement_Click;

                if (i == _ViewModel.SelectedContent)
                {
                    //Kiválasztott gomb
                    DynamicListButtons[i].Background = (Brush)new BrushConverter().ConvertFromString("#71D161"); 
                }
                else
                {
                    //Nem kiválasztott gombok
                    DynamicListButtons[i].Background = (Brush)new BrushConverter().ConvertFromString("#49A34B");
                }

                DynamicListButtons[i].BorderThickness = new Thickness(0);
                ContentList.Children.Add(DynamicListButtons[i]);

                //if (_ViewModel.GetSelectedContent().Pictures != null)
                //{
                //    ContentImagePanel = new StackPanel();
                //    ContentImagePanel.Height = 20;
                //    ContentImagePanel.HorizontalAlignment = HorizontalAlignment.Stretch;
                //    ContentImagePanel.VerticalAlignment = VerticalAlignment.Bottom;
                //    ContentImagePanel.Background = Brushes.Black;
                //    Grid.SetRow(ContentImagePanel, 1);
                //    ContentViewGrid.Children.Add(ContentImagePanel);

                //    //ContentImage = new Image();
                //    //ContentImage.HorizontalAlignment = HorizontalAlignment.Right;
                //    //ContentImage.VerticalAlignment = VerticalAlignment.Top;
                //    //ContentImage.Height = 100;
                //    //ContentImage.Width = 100;
                //    //ContentImage.Source = new BitmapImage(new Uri(@"Images/" + _ViewModel.GetSelectedContent().Pictures[0], UriKind.RelativeOrAbsolute));
                //}

            }
        }

        private void AddImageButton_Click(object sender, RoutedEventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "Image files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                string PictureFilePath = openFileDialog1.SafeFileName;
                if (!_ViewModel.AddImageToContent(PictureFilePath))
                {
                    MessageBox.Show("Somthing happend ¯\\_(ツ)_/¯");
                    MessageBox.Show("NOTICE ME SENPAI!!!");
                }
            }
        }

        private void RemoveImageButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("NOTICE ME SENPAI!!!");
        }
    }
}
